package net.xdclass.controller;


import com.google.code.kaptcha.Producer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.service.NotifyService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


@Slf4j
@Api(tags = "通知模块")
@RestController
@RequestMapping("/api/user/v1")
public class NotifyController {


    @Resource
    private Producer captchaProducer;

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    private NotifyService notifyService;


    //有效期十分钟
    private static  final  long CAPTCHA_CODE_EXPIREO=60*1000*10;

    @ApiOperation("获取图形验证码")
    @GetMapping("captcha")
    public  void  getCaptcha(HttpServletRequest request, HttpServletResponse response){


        String captcha = captchaProducer.createText();
        log.info("图形验证码"+captcha);
        String captchaKey = this.getCaptchaKey(request);

        redisTemplate.opsForValue().set(captchaKey,captcha);

        BufferedImage image = captchaProducer.createImage(captcha);
        ServletOutputStream outputStream = null;
        try{

           outputStream= response.getOutputStream();
            ImageIO.write(image,"jpg",outputStream);
            outputStream.flush();
            outputStream.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    /**
     * 1:匹配图形验证码是否正常
     * 2：发送验证码
     * @param to
     * @param captcha
     * @param request
     */
    @ApiOperation("发送邮箱验证码")
    @GetMapping("send_code")
    public JsonData sendRegisterCode(@RequestParam(value = "to")String to,
                                 @RequestParam(value = "captcha")String captcha,
                                 HttpServletRequest request){

        String key= getCaptchaKey(request);
        String cacheCaptcha=redisTemplate.opsForValue().get(key);

        //匹配验证码是否一样
        if (captcha!=null&&cacheCaptcha!=null&&captcha.equals(cacheCaptcha)){

            //成功
            redisTemplate.delete(key);
            JsonData jsonData = notifyService.sendCode(to, SendCodeEnum.USER_REGISTER);
            return jsonData;
        }else {

          return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
        }


    }


    /**
     * 获取缓存的KEY
     * @param request
     * @return
     */
    private  String getCaptchaKey(HttpServletRequest request){

        String ip = CommonUtil.getIpAddr(request);

        String userAgent = request.getHeader("User-Agent");

        String key="user-service:captcha:"+CommonUtil.MD5(ip+userAgent);
        log.info("ip"+ip);
        log.info("key"+key);
        log.info("userAgent"+userAgent);

        return key;
    }
}
